package com.zjl.workflow.config;

import com.zjl.workflow.listener.GlobalProcessStartedListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.Resource;

/**
 * @module
 * @ClassDesc 功能描述: flowable全局监听器配置
 * @Author huangjk
 * @create 2021/10/20 16:23
 **/
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {


    @Resource
    private SpringProcessEngineConfiguration configuration;

    @Resource
    private GlobalProcessStartedListener globalProcessStartedListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        if (globalProcessStartedListener != null) {
            // 添加流程创建全局监听
            dispatcher.addEventListener(globalProcessStartedListener, FlowableEngineEventType.PROCESS_STARTED);
        }
    }

}
